import tkinter as tk
from tkinter.constants import N, NE, NS, NW, SE, SW
from PIL import ImageTk, Image
import rospy
from rospy.timer import sleep
from rospy.topics import Publisher
from sensor_msgs.msg import JointState
from std_msgs.msg import Header
from std_msgs.msg import String
#from body_state.msg import body_state
window = tk.Tk()
window.title("sillyDog")
window.geometry('1100x600')


#--ros_control
robot_stand_pub=rospy.Publisher('robot_stand',String,queue_size=2)
robot_walk_pub=rospy.Publisher('robot_walk',String,queue_size=2)
robot_joint_pub=rospy.Publisher('/joint_states',JointState,queue_size=10)
#robot_body_state_pub=rospy.Publisher('body_states',body_state,queue_size=10)
rospy.init_node('control_sillydog',anonymous=True)
rate = rospy.Rate(10)
def pub_robot_stand():
    robot_stand_pub.publish("stand")
    print("stand")
    rate.sleep()
def pub_robot_walk():
    robot_walk_pub.publish("walk")
    print("walk")
    rate.sleep()
control_thread = JointState()
control_thread.header = Header()
control_thread.name = ['joint_FR1','joint_FR2','joint_BR1','joint_BR2','joint_BL1','joint_BL2','joint_FL1','joint_FL2']
control_thread.velocity = []
control_thread.effort = []
def send_jointstates():
    control_thread.header.stamp = rospy.Time.now()
    control_thread.position = [0.785,1.571,0.785,1.571,0.785,1.571,0.785,1.571]
    pub.publish(control_thread)









#------------------------------------------------------------------the view 
canvas = tk.Canvas(window, bg='green', height=600, width=1100)
image_file = tk.PhotoImage(file='/home/wmrobot/silly-dog/image/finalRobot.gif') 
canvas.create_image(550, 0, anchor='n',image=image_file)   
canvas.place(x=0,y=0,anchor='nw')

var_roll= tk.StringVar()
roll = tk.Label(window,text='',textvariable=var_roll,bg='green',font=('Arial', 12), width=10, height=2)
roll_lable = tk.Label(window,text='roll:',bg='green',font=('Arial', 12), width=10, height=2)
roll_lable.place(x=820,y=190,anchor='nw')
roll.place(x=930,y=190,anchor='nw')
var_pitch= tk.StringVar()
pitch = tk.Label(window,text='',textvariable=var_pitch,bg='green',font=('Arial', 12), width=10, height=2)
pitch_label = tk.Label(window,text='pitch',bg='green',font=('Arial', 12), width=10, height=2)
pitch.place(x=930,y=260,anchor='nw')
pitch_label.place(x=820,y=260,anchor='nw')
var_yaw= tk.StringVar()
yaw = tk.Label(window,text='',textvariable=var_yaw,bg='green',font=('Arial', 12), width=10, height=2)
yaw_lable = tk.Label(window,text='yaw:',bg='green',font=('Arial', 12), width=10, height=2)
yaw.place(x=930,y=330,anchor='nw')
yaw_lable.place(x=820,y=330,anchor='nw')
var_temperature = tk.StringVar()
temperature = tk.Label(window,text='',textvariable=var_temperature,bg='green',font=('Arial', 12), width=10, height=2)
temperature_lable = tk.Label(window,text='temperature:',bg='green',font=('Arial', 12), width=12, height=2)
temperature.place(x=930,y=400,anchor='nw')
temperature_lable.place(x=820,y=400,anchor='nw')
roll_vaule = tk.Entry(window, font=('Arial', 14),width=5)
roll_vaule.place(x=950,y=480,anchor='nw')
roll_vaule_label = tk.Label(window,text='roll:',font=('Arial', 14), bg='green', width=8, height=1)
roll_vaule_label.place(x=820,y=480)
pitch_vaule = tk.Entry(window, font=('Arial', 14),width=5)
pitch_vaule.place(x=950,y=520,anchor='nw')
pitch_vaule_label = tk.Label(window,text='pitch:',font=('Arial', 14), bg='green', width=8, height=1)
pitch_vaule_label.place(x=820,y=520)
yaw_vaule = tk.Entry(window, font=('Arial', 14),width=5)
yaw_vaule.place(x=950,y=560,anchor='nw')
yaw_vaule_label = tk.Label(window,text='yaw:',font=('Arial', 14), bg='green', width=8, height=1)
yaw_vaule_label.place(x=820,y=560)
var_roll.set(1)
var_pitch.set(2)
var_yaw.set(3)


def hit_funcation():
    print('hit')
stand = tk.Button(window, text= "起立" ,font=('Arial',12), width=10, height=1, command=pub_robot_stand)
stand.place(x=850,y=20,anchor='nw')
walk = tk.Button(window, text= "慢走" ,font=('Arial',12), width=10, height=1, command=pub_robot_walk)
walk.place(x=850,y=70,anchor='nw')
trot = tk.Button(window, text= "小跑" ,font=('Arial',12), width=10, height=1, command=hit_funcation)
trot.place(x=850,y=120,anchor='nw')


joint_FR1 = tk.Scale(window,label="前右大腿",from_=0, to=180, orient=tk.HORIZONTAL, length=800, showvalue=0, tickinterval=10,resolution=0.01)
joint_FR1.place(x=0,y=0,anchor=NW)
joint_FR2 = tk.Scale(window,label="前右小腿",from_=0, to=180, orient=tk.HORIZONTAL, length=800, showvalue=0, tickinterval=10,resolution=0.01)
joint_FR2.place(x=0,y=70,anchor=NW)
joint_BL1 = tk.Scale(window,label="后左大腿",from_=0, to=180, orient=tk.HORIZONTAL, length=800, showvalue=0, tickinterval=10,resolution=0.01)
joint_BL1.place(x=0,y=140,anchor=NW)
joint_BL2 = tk.Scale(window,label="后左小腿",from_=0, to=180, orient=tk.HORIZONTAL, length=800, showvalue=0, tickinterval=10,resolution=0.01)
joint_BL2.place(x=0,y=210,anchor=NW)
joint_BR1 = tk.Scale(window,label="后右大腿",from_=0, to=180, orient=tk.HORIZONTAL, length=800, showvalue=0, tickinterval=10,resolution=0.01)
joint_BR1.place(x=0,y=280,anchor=NW)
joint_BR2 = tk.Scale(window,label="后右小腿",from_=0, to=180, orient=tk.HORIZONTAL, length=800, showvalue=0, tickinterval=10,resolution=0.01)
joint_BR2.place(x=0,y=350,anchor=NW)
joint_FL1 = tk.Scale(window,label="前左大腿",from_=0, to=180, orient=tk.HORIZONTAL, length=800, showvalue=0, tickinterval=10,resolution=0.01)
joint_FL1.place(x=0,y=420,anchor=NW)
joint_FL2 = tk.Scale(window,label="前左小腿",from_=0, to=180, orient=tk.HORIZONTAL, length=800, showvalue=0, tickinterval=10,resolution=0.01)
joint_FL2.place(x=0,y=490,anchor=NW)


window.mainloop()
